#include"queue.h"
void QueueInit(Que* ps)
{
	ps->head = ps->tail = NULL;
	ps->size = 0;


}
void QueuePush(Que* ps, DataType x)
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(struct QueueNode));
	if (newnode == NULL)
	{
		perror("malloc:");
		exit(-1);
	}
	newnode->date = x;
	newnode->next = NULL;

	if (ps->tail == NULL)
	{
		ps->head = newnode;
		ps->tail - newnode;
	}
	else
	{
		ps->tail = newnode;
		ps->tail->next = newnode;

	}
	++ps->size;


}
void  QueuePop(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = NULL;
		ps->tail = NULL;
	}
	else
	{

		QNode* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}
	--ps->size;


}
void QueueDestroy(Que* ps)
{
	assert(ps);
	QNode* cur = ps->head;
	while (cur)
	{
		QNode* newnext =cur->next;
		free(cur);
		cur = newnext;
	}
}
int QueueFront(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->head->date;


}
int QueueBack(Que* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->tail->date;
}
int QueueSize(Que* ps)
{
	return ps->size;
}
bool QueueEmpty(Que* ps)
{
	return ps->head == NULL;
}
//stack
void STInit(ST* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;

}
void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->capacity = ps->top = 0;


}
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	STDataType newcapacity = 0;

	if (ps->capacity == ps->top)
	{
		newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, sizeof(ST) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc:");
			exit(-1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;

	}
	//ps->a = temp;
	ps->a[ps->top] = x;
	ps->capacity = newcapacity;
	ps->top++;


}
void  STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	--ps->top;


}
STDataType STTop(ST* ps)
{

	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];

}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
bool STEmpty(ST* ps)
{
	return ps->top == 0;
}